package com.icarsaas.saas.tenant.system.service.impl;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.icarsaas.saas.common.data.PageSort;
import com.icarsaas.saas.common.page.SoulPage;
import com.icarsaas.saas.tenant.system.model.ActionLog;
import com.icarsaas.saas.tenant.system.repository.ActionLogRepository;
import com.icarsaas.saas.tenant.system.service.ActionLogService;

@Service
public class ActionLogServiceImpl implements ActionLogService {

    @Autowired
    private ActionLogRepository actionLogRepository;

    /**
     * 根据日志ID查询日志数据
     * @param id 日志ID
     */
    @Override
    @Transactional("tenantTransactionManager")
    public ActionLog getById(Long id) {
        return actionLogRepository.findById(id).orElse(null);
    }

    /**
     * 获取分页列表数据
     * @param example 查询实例
     * @return 返回分页数据
     */
    @Override
    public Page<ActionLog> getPageList(Example<ActionLog> example) {
        // 创建分页对象
        PageRequest page = PageSort.pageRequest();
        return actionLogRepository.findAll(example, page);
    }

    /**
     * 获取数据的日志列表
     * @param model 模型（表名）
     * @param recordId 数据ID
     */
    @Override
    public List<ActionLog> getDataLogList(String model, String recordId){
        return actionLogRepository.findByModelAndRecordId(model, recordId);
    }

    /**
     * 保存日志
     * @param actionLog 日志实体类
     */
    @Override
    public ActionLog save(ActionLog actionLog){
        return actionLogRepository.save(actionLog);
    }

    /**
     * 删除指指定ID日志
     */
    @Override
    @Transactional("tenantTransactionManager")
    public void deleteId(Long id){
        actionLogRepository.deleteById(id);
    }

    /**
     * 清空日志
     */
    @Override
    @Transactional("tenantTransactionManager")
    public void emptyLog(){
        actionLogRepository.deleteAll();
    }

	@Override
	public SoulPage<Map<String, Object>> listData(SoulPage<Map<String, Object>> page, Map<String, Object> params) {
		StringBuffer sql = new StringBuffer("select * from sys_action_log t where 1=1");//多表联合查询可以多包一层
		if (params != null && params.size() > 0) {
			for (String key : params.keySet()) {
				String value = (String) params.get(key);
				if (StringUtils.isNotBlank(value)) {
                	if (key.equalsIgnoreCase("startDate")) {
						sql.append(" and t.create_date >='" + value + " 00:00:00'");
					} else if (key.equalsIgnoreCase("endDate")) {
						sql.append(" and t.create_date <='" + value  + " 23:59:59'");
					} else sql.append(" and t." + key + " like '%" + value + "%'");
                }
			}
		}
		SoulPage<Map<String, Object>> pageResult = actionLogRepository.findBySqlForSoul(page, sql.toString(), Map.class);
		return pageResult;
	}
}
